package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.dataservice.cache.ProductCache;
import com.iwhalecloud.bss.kite.domain.inst.repository.SceneInstRepository;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.*;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.common.exception.BssException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class ProductModeAttrHandler extends AbstractAttrHandler<KiteInst, AttrData> {
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void init(KiteAttr attr, KiteInst inst) {
        inst.getAttrMap();
        super.init(attr, inst);
    }

    @Override
    public void set(KiteAttr attr, KiteInst inst, String value, AttrData data) {
        if(KiteStringUtils.equals("210004659",attr.getFieldName())){//服务模式判断是否能更改
            List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncs(data.getCustId(),inst.getInstId());
            if(prodInstFuncs.size()>0){
                BssException.throwOut(KeyConsts.LOG_MODULE, "",String.format("已存在【%s】对应的产品模式",attr.getValueDesc()));
            }
        }
        super.set(attr, inst, value, data);
        /*List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncs(data.getCustId(),inst.getInstId());
        if(KiteListUtils.isNotEmpty(prodInstFuncs)){
            prodInstFuncs.forEach(prodInstFunc->{
                if ("300000209".equals(ProductCache.get(prodInstFunc.getProdId()).getProdNbr())) {
                    KiteAttr cpmsAttr = prodInstFunc.getAttr("210004660");
                    String attr_id = "20210622705";
                    if(KiteStringUtils.equals("110008284",value)){
                        attr_id = "20210622704";
                    }
                    List<AttrValue> attrValues = AttrValueCache.get(attr_id);
                    List<KiteAttrValue> attrValueList = new ArrayList<>();
                    attrValues.forEach(avo->{
                        KiteAttrValue kiteAttrValue = new KiteAttrValue();
                        //kiteAttrValue.set
                    });
                    //cpmsAttr.setAttrValues();
                    IOperateSceneInstService operateSceneInstService = SpringUtil.getBean(IOperateSceneInstService.class);
                    operateSceneInstService
                        .setAttrs(data.getSceneInstId(), prodInstFunc.getInstType(), prodInstFunc.getInstId(), prodInstFunc.listAttrs());
                }
            });
        }*/
    }

    @Override
    public void after(KiteAttr attr, KiteInst inst, AttrData data) {
        List<KiteOfferInst> kiteOfferInsts = querySceneInstService.listMainOfferInsts(data.getSceneInstId());
        if(KiteListUtils.isNotEmpty(kiteOfferInsts)){
            KiteOfferInst kiteOfferInst = kiteOfferInsts.get(0);
            String offerInstId = kiteOfferInst.getOfferInstId();
            List<KiteProdInst> prodInsts = querySceneInstService.listProdInstsFromOfferInst(data.getCustId(), offerInstId);
            KiteProdInst prodInst = prodInsts.get(0);
            List<KiteProdInstFunc> prodInstFuncs = querySceneInstService.listProdInstFuncs(data.getCustId(),prodInst.getInstId());//主产品下的功能产品
            List<String> filterId = new ArrayList<>();
            if(KiteListUtils.isNotEmpty(prodInstFuncs)){
                prodInstFuncs.forEach(prodInstFunc->{
                    if (!KiteStringUtils.equals(inst.getInstId(),prodInst.getInstId())&&"300000209".equals(ProductCache.get(prodInstFunc.getProdId()).getProdNbr())) {
                        filterId.add(prodInstFunc.getAttrValue("210004660"));
                    }
                });
            }
            KiteAttr prodModAttr = prodInst.getAttr("210004659");//服务模式
            String attr_id = "20210622704";
            if(KiteStringUtils.equals("110008285",prodModAttr.getValue())){
                attr_id = "20210622705";
            }
            KiteAttr kiteAttr = new KiteAttr();
            kiteAttr.setAttrValueTypeId("98A");
            kiteAttr.setIsNull("F");
            kiteAttr.setAttrId(attr_id);
            List<KiteAttrValue> kiteAttrValues = SceneInstRepository.getKiteAttrValues(kiteAttr);
            if(KiteListUtils.isNotEmpty(filterId)){
                kiteAttrValues = kiteAttrValues.stream().filter(kts->!filterId.contains(kts.getAttrValue())).collect(Collectors.toList());
            }
            if(kiteAttrValues.size() == 0){
                BssException.throwOut(KeyConsts.LOG_MODULE, "","没有可选的产品模式");
            }
            attr.setAttrValues(kiteAttrValues);
            if(kiteAttrValues.size()> 0){
                KiteAttrValue kv = kiteAttrValues.get(0);
                attr.setValue(kv.getAttrValue());
                attr.setValueDesc(kv.getAttrValueName());
            }

        }
        super.after(attr, inst, data);
    }
}
